<?php

namespace App\Http\Controllers\Admin;

//use App\Config;
use App\Http\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Intervention\Image\Facades\Image;

class UploadController extends Controller
{
    use Response;
    public function index(Request $request)
    {
       $file=$request->file("file");
       $allowed_ext=['jpg','jpeg','png','gif'];
       $allowed_ext2=['mp4','3pg','m3u8'];
       if ($file->isValid()){
           $extention=$file->getClientOriginalExtension();
           if (!in_array($extention,$allowed_ext)&&!in_array($extention,$allowed_ext2)){
               return $this->error(505,'格式错误，无法上传');
           }
           $name=uniqid();
           if (in_array($extention,$allowed_ext)){
               $des="images/upload";
           }else{
               $des="videos/upload";
           }

           $file->move($des,$name.'.'.$extention);
//           $short=$name.'_s'.".".$extention;
//           $config=$this->getResize();
//           if (!$config){
               $config=[
                   'width'=>310,
                   'height'=>296,
                   'domain'=>$request->header('domain')
               ];
//           }
//           Image::make($des.'/'.$name.'_d.'.$extention)->resize($config['width'],$config['height'])->save($des.'/'.$short);
           return $this->success(200,\config("app.url").'/'.$des.'/'.$name.'.'.$extention);



       }
    }
//    private function getResize()
//    {
//        $config=Config::where([['mch_id','=',auth('merchant')->id()],['title','=','picture']])->first();
//
//        return $config['value'];
//    }
}
